#include "common.h"

namespace fireplane
{
	CCSize Common::getWinSize()
	{
		return CCSizeMake(640,960);
	}

	CCNode* Common::createScaleNode(CCScene* scene)
	{
#if 0
		CCNode* mnode0 = CCNode::create();
		mnode0->setPosition(ccp(160,240));
		scene->addChild(mnode0);

		CCNode* snode = CCNode::create();
		snode->setScale(0.5f);
		mnode0->addChild(snode);

		CCNode* mnode = CCNode::create();
		mnode->setPosition(ccp(-320,-480));
		snode->addChild(mnode);

		return mnode;
#else
		CCNode* node =  CCNode::create();
		scene->addChild(node);

		return node;
#endif
	}

	CCMenuItemSprite* Common::itemFromNormalImage(const char *normalImage, 
		const char *selectedImage,
		CCObject* target, SEL_MenuHandler selector,bool needScale)
	{
		CCSprite* normal;
		CCSprite* select;

		CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
		
		CCSpriteFrame* frame = cache->spriteFrameByName(normalImage);
		normal = CCSprite::createWithSpriteFrame(frame);
		normal->setAnchorPoint(ccp(0.5f,0.5f));

		frame = cache->spriteFrameByName(selectedImage);
		select = CCSprite::createWithSpriteFrame(frame);
		select->setAnchorPoint(ccp(0.5f,0.5f));
		if (needScale)
		{
			select->setScale(1.2f);
		}

		CCMenuItemSprite* ret = CCMenuItemSprite::create(normal,select,target,selector);
		
		return ret;
	}

	void Common::doLoadLogic( string fileName )
	{
		if (fileName.find(".png") != -1 || fileName.find(".jpg") != -1)
		{
			CCTextureCache::sharedTextureCache()->addImage(fileName.c_str());
		}
		else if(fileName.find(".plist") != -1)
		{
			CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(
				fileName.c_str());
		}
	}

	CCSprite* Common::createSpriteByFrameName( const char* frameName )
	{
		CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
		CCSpriteFrame* frame = cache->spriteFrameByName(frameName);
		CCSprite* ret = CCSprite::createWithSpriteFrame(frame);

		return ret;
	}

	CCPoint Common::CCPointApplyAffineTransform(CCPoint& vector,const CCAffineTransform& t){
		CCPoint p;
		p.x = (float)((double)t.a * vector.x + (double)t.c * vector.y);
		p.y = (float)((double)t.b * vector.x + (double)t.d * vector.y);
		return p;
	}

	CCActionInterval* Common::createFrameAnimation( string pre,int count,CCRect rect )
	{
		CCAnimate* action = NULL;
		CCAnimation* animation = NULL;
		CCArray* allframes = CCArray::create();
		CCSpriteFrame* frmae = NULL;

		for (int i = 0;i < count;i ++)
		{
			char buffer[0xff];
			sprintf(buffer,"%s%d.png",pre.c_str(),i);
			frmae = CCSpriteFrame::create(buffer,rect);
			allframes->addObject(frmae);
		}

		animation = CCAnimation::createWithSpriteFrames(allframes,0.03f);
		action = CCAnimate::create(animation);

		return action;
	}

}


